package cn.edu.njuit.lesson9heros.aspect;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DemoAspect {
    @Pointcut("execution(* cn.edu.njuit.lesson9heros.service.impl..*(..))")
    public void gamePointCut(){}

    @DeclareParents(value = "cn.edu.njuit.lesson9heros.service.impl..*", defaultImpl = LogServiceImpl.class)
    private LogService logService;

    @Before("this(logService)")
    public void logBefore(JoinPoint joinPoint,LogService logService){
        System.out.println("logBefore通知执行");
        Class c=joinPoint.getTarget().getClass();
        String methodName=joinPoint.getSignature().getName();
        Object [] args=joinPoint.getArgs();
        logService.doLog(c,methodName,args);
    }



    @Pointcut("execution(* cn.edu.njuit.lesson9heros.service.TeamService.*(..))")
    public void teamPointCut(){}

    @AfterReturning(pointcut = "teamPointCut()",returning = "result")
    public void teamAfterPointCut(JoinPoint joinPoint,Object result){
        System.out.println("TeamService组队成功后通知:"+ JSON.toJSONString(result));
    }

    //定义切点
    @Pointcut("execution(* cn.edu.njuit.lesson9heros.service.GameService.play(..))")
    public void gamePlayPointCut(){}
    //环绕通知
    @Around("gamePlayPointCut()")
    public void playAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] params=joinPoint.getArgs();
        if(params.length!=2 || params[0]==null || params[1]==null){
            throw new Exception("游戏Play参数条件不具备");
        }else{
            //放行
            joinPoint.proceed();
        }
    }





}
